using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;


namespace MinimizeEngine.Managers
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class MEAudioManager : Microsoft.Xna.Framework.GameComponent
    {
        #region Fields

        private static Dictionary<string, SoundEffect> mLoadedSoundEffects = new Dictionary<string, SoundEffect>();
        private static Dictionary<string, Song> mLoadedSongs = new Dictionary<string, Song>();

        #endregion

        #region Properties

        #region Songs

        /// <summary>
        /// Load a Song into the Minimize Engine Audio Manager
        /// </summary>
        /// <param name="soundName">Name of the Song</param>
        /// <param name="pathName">Path of the Song to be loaded in</param>
        public static void LoadSong(string soundName, string pathName)
        {
            if (!string.IsNullOrEmpty(soundName) && !string.IsNullOrEmpty(pathName))
            {
                mLoadedSongs.Add(soundName, MEEngineManager.ContentManager.Load<Song>(pathName));
            }
        }

        /// <summary>
        /// Is the Audio Manager Playing a Song
        /// </summary>
        /// <returns></returns>
        public static bool IsPlaying()
        {
            if(MediaPlayer.State == MediaState.Playing)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// Is the Audio Manager Paused
        /// </summary>
        /// <returns></returns>
        public static bool IsPaused()
        {
            if (MediaPlayer.State == MediaState.Paused)
            {
                return true;
            }
            return false;
        }
        /// <summary>
        /// Is the Audio Manager Stopped
        /// </summary>
        /// <returns></returns>
        public static bool IsStopped()
        {
            if (MediaPlayer.State == MediaState.Stopped)
            {
                return true;
            }
            return false;
        }


        /// <summary>
        /// Play a Song from the Minimize Engine Audio Manager
        /// </summary>
        /// <param name="soundName">Name of the Song to be Played</param>
        public static void PlaySong(string soundName)
        {
            if (!string.IsNullOrEmpty(soundName))
            {
                if(MediaPlayer.State == MediaState.Stopped)
                {
                    MediaPlayer.Play(mLoadedSongs[soundName]);
                }
            }
        }

        /// <summary>
        /// Move to the next Song in the Media Player's Queue
        /// </summary>
        public static void NextSong()
        {
            MediaPlayer.MoveNext();
        }

        /// <summary>
        /// Move to the previous Song in the Media Player's Queue
        /// </summary>
        public static void PreviousSong()
        {
            MediaPlayer.MovePrevious();
        }

        /// <summary>
        /// Shuffle the Media Players Songs
        /// </summary>
        /// <param name="shuffle"></param>
        public static void SetShuffle(bool shuffle)
        {
            MediaPlayer.IsShuffled = shuffle;
        }

        /// <summary>
        /// Repeat the currently Playing Song
        /// </summary>
        /// <param name="repeat"></param>
        public static void SetRepeated(bool repeat)
        {
            MediaPlayer.IsRepeating = repeat;
        }

        /// <summary>
        /// Stops the Audio Playback
        /// </summary>
        public static void StopPlaying()
        {
            MediaPlayer.Stop();
        }

        /// <summary>
        /// Pause the currently Playing Song.
        /// </summary>
        public static void PausePlaying()
        {
            MediaPlayer.Pause();
        }

        /// <summary>
        /// Resumes the Playing of the currently Paused Song.
        /// </summary>
        public static void ResumePlaying()
        {
            MediaPlayer.Resume();
        }

        #endregion

        #region Sound Effects

        /// <summary>
        /// Load a Sound Effect into the Minimize Engine Audio Manager
        /// </summary>
        /// <param name="soundName">Name of the Sound Effect</param>
        /// <param name="pathName">Path of the Sound Effect to be loaded in</param>
        public static void LoadSoundEffect(string soundName, string pathName)
        {
            if (!string.IsNullOrEmpty(soundName) && !string.IsNullOrEmpty(pathName))
            {
                mLoadedSoundEffects.Add(soundName, MEEngineManager.ContentManager.Load<SoundEffect>(pathName));
            }
        }

        /// <summary>
        /// Play a Sound Effect from the Minimize Engine Audio Manager
        /// </summary>
        /// <param name="soundName">Name of the Sound Effect to be Played</param>
        public static void PlaySoundEffect(string soundName)
        {
            if (!string.IsNullOrEmpty(soundName))
            {
                mLoadedSoundEffects[soundName].CreateInstance().Play();
            }
        }

        /// <summary>
        /// Stop a Sound Effect 
        /// </summary>
        /// <param name="soundName">Name of the Sound Effect to be Stopped</param>
        public static void StopSoundEffect(string soundName)
        {
            if (!string.IsNullOrEmpty(soundName))
            {
                mLoadedSoundEffects[soundName].CreateInstance().Stop();
            }
        }

        /// <summary>
        /// Pause a Sound Effect
        /// </summary>
        /// <param name="soundName">Name of the Sound Effect to be Paused</param>
        public static void PauseSoundEffect(string soundName)
        {
            if (!string.IsNullOrEmpty(soundName))
            {
                mLoadedSoundEffects[soundName].CreateInstance().Pause();
            }
        }

        #endregion

        #endregion

        #region Initialization

        /// <summary>
        /// Construct a new Minimize Engine Audio Manager
        /// </summary>
        /// <param name="game"></param>
        public MEAudioManager(Game game)
            : base(game)
        {

        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            base.Initialize();
        }

        #endregion
    }
}